package com.alphathink.BitMapUtil;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
/**
 * 自定义lrucache类。继承自{@link BitmapLruCache},主要是实现了sizeOf函数。
 * @author zyn
 *
 */
public class BitmapLruCache extends LruCache<String, Bitmap> {
	private static BitmapLruCache lruCache = null;

	public static BitmapLruCache getInstance() {
		if (lruCache == null) {
			lruCache = new BitmapLruCache((int) (Runtime.getRuntime()
					.maxMemory() / (1024 * 10)));
		}
		return lruCache;
	}

	private BitmapLruCache(int maxSize) {
		super(maxSize);
	}

	@Override
	@SuppressLint("NewApi")
	protected int sizeOf(String key, Bitmap value) {
		if (android.os.Build.VERSION.SDK_INT >= 19) {
			value.getAllocationByteCount();
		} else if (android.os.Build.VERSION.SDK_INT < 19
				&& android.os.Build.VERSION.SDK_INT >= 12) {
			return value.getByteCount() / 1024;
		}
		return value.getRowBytes() * value.getHeight() / 1024;
	}

}
